package com.xiaodu.servlet;

import com.xiaodu.filter.MyServletFilter2;
import com.xiaodu.listener.MyServletContentListener;
import com.xiaodu.listener.MyServletContentListener2;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: dutianjie
 * @date: 2021/4/15
 * @version: v1.0.0
 * @description: 描述
 **/
@Configuration
public class ServletConfig {

    @Bean
    public ServletRegistrationBean<MyServlet2> servletRegistrationBean() {
        ServletRegistrationBean<MyServlet2> servletRegistrationBean = new ServletRegistrationBean<>();
        servletRegistrationBean.setServlet(new MyServlet2()); // 设置servlet
        servletRegistrationBean.addUrlMappings("/test/servlet2"); // url
        servletRegistrationBean.setLoadOnStartup(0); // 启动加载顺序
        servletRegistrationBean.setAsyncSupported(true); // 是否支持异步
        servletRegistrationBean.setName("myServlet2"); // servlet-name
        // init-param
        Map<String, String> initParam = new HashMap<>();
        initParam.put("param", "value");
        servletRegistrationBean.setInitParameters(initParam);
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<MyServletFilter2> filterRegistrationBean() {
        FilterRegistrationBean<MyServletFilter2> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MyServletFilter2());
        // 指定过滤路径
        registrationBean.addUrlPatterns("/test/*");
        // 指定过滤servletRegistrantionBean
//        registrationBean.addServletRegistrationBeans(servletRegistrationBean());// 可以过滤那个servlet
        // 指定过滤servlet
//        registrationBean.addServletNames("myServlet2");
        // init filter param
        registrationBean.addInitParameter("filter2", "filter2-value");
        return registrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean<MyServletContentListener2> servletListenerRegistrationBean() {
        ServletListenerRegistrationBean<MyServletContentListener2> servletListenerRegistrationBean = new ServletListenerRegistrationBean<>();
        servletListenerRegistrationBean.setListener(new MyServletContentListener2());
        return servletListenerRegistrationBean;
    }


}
